package com.drinks.yiniang.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.dialog.NumPadAdapter;
import com.hansen.library.Constants;
import com.hansen.library.listener.OnNumPadInputListener;
import com.hansen.library.listener.OnSureCancelListener;
import com.hansen.library.utils.LogUtils;

/**
 * @author han
 * @date 2019/11/4
 */
public class NumpadDialog extends DialogFragment implements AdapterView.OnItemClickListener {
    private final static int NUMBER_BUTTON_DELETE = 11;//删除
    private final static int NUMBER_BUTTON_ZERO = 10;//0号按键
    private final static int NUMBER_BUTTON_CLEAR = 9;//清除按键

    private Context mContext;

    private TextView tv_numpad_done;
    private GridView gv_numpad_keyboard;

    private NumPadAdapter mNumPadAdapter;

    private OnNumPadInputListener onNumPadInputListener;

    private String mValue;
    private String mParams;
    private int mPosition = -1;

    public static NumpadDialog newInstance() {
        return new NumpadDialog();
    }

    public static NumpadDialog newInstance(int position, String params) {

        Bundle bundle = new Bundle();
        bundle.putInt(Constants.KeyPos, position);
        bundle.putString(Constants.KeyTextContents, params);

        NumpadDialog dialog = new NumpadDialog();
        dialog.setArguments(bundle);

        return dialog;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            if(getParentFragment() != null) {
                onNumPadInputListener = (OnNumPadInputListener) getParentFragment();
            } else {
                onNumPadInputListener = (OnNumPadInputListener) context;
            }
        } catch (ClassCastException e) {
            LogUtils.e(context.toString() + " must implement OnNumPadInputListener");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mContext = getActivity();

        LayoutInflater inflater = this.getActivity().getLayoutInflater();

        View mView = inflater.inflate(R.layout.dialog_numpad, null);

        Dialog dialog = new Dialog(this.getActivity(), R.style.PayDialogStyle);

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(mView);
        dialog.setCanceledOnTouchOutside(true);

        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.dimAmount = 0.5f;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平

        dialogWindow.setAttributes(lp);
        dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        tv_numpad_done = mView.findViewById(R.id.tv_numpad_done);

        gv_numpad_keyboard = mView.findViewById(R.id.gv_numpad_keyboard);

        initData();

        return dialog;
    }

    private void initData() {

        mPosition = getArguments() != null ? getArguments().getInt(Constants.KeyPos, -1) : -1;
        mParams = getArguments() != null ? getArguments().getString(Constants.KeyTextContents, "") : "";

        mNumPadAdapter = new NumPadAdapter(mContext);

        gv_numpad_keyboard.setAdapter(mNumPadAdapter);
        gv_numpad_keyboard.setOnItemClickListener(this);

        tv_numpad_done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        Fragment mFragment = manager.findFragmentByTag(tag);
        //防止java.lang.IllegalStateException: Fragment already added
        if (mFragment == null || !mFragment.isAdded()) {
            super.show(manager, tag);
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if(onNumPadInputListener != null) {
            onNumPadInputListener.onNumPadInputSuccess(mPosition, mParams, mValue, false);
        }
        super.onDismiss(dialog);
    }

    @Override
    public void dismiss() {
        //super.dismiss();
        dismissAllowingStateLoss();
    }

    @Override
    public void onDestroy() {
        onNumPadInputListener = null;
        super.onDestroy();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(mValue == null) {
            mValue = "";
        }

        if (position == NUMBER_BUTTON_CLEAR) {
            mValue = "";
        } else if (position == NUMBER_BUTTON_DELETE) {
            if (mValue.length() > 0) {
                mValue = mValue.substring(0, mValue.length() - 1);
            }
        } else {
            if (position == NUMBER_BUTTON_ZERO) {
                mValue += "0";
            } else {
                mValue += (position + 1);
            }
        }

        if (mValue.startsWith("0")) {
            mValue = mValue.substring(1, 1);
        }

        LogUtils.e("mValue===" + mValue);

        if(onNumPadInputListener != null) {
            onNumPadInputListener.onNumPadInputSuccess(mPosition, mParams, mValue, true);
        }
    }
}
